<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>编辑{{ detailType == 1 ? '评测分类' : '评测项' }}</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="infoForm" label-width="100px" ref="editForm">
          <el-form-item label="名称：" prop="num">
            <el-input v-model="infoForm.name" placeholder="名称" size="small"></el-input>
          </el-form-item>
          <el-form-item label="评测范围：" v-if="detailType==1">
            <el-select size="small" placeholder="评测范围" v-model="section_ids" class="mr-10" multiple>
              <el-option :label="item.section_name" :value="item.id" v-for="(item, index) in tableList"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="评测类型：" v-if="detailType==2">
            <el-radio-group v-model="infoForm.type">
              <el-radio :label="1">计时</el-radio>
              <el-radio :label="2">数值</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="评测描述：" v-if="detailType==2">
            <el-input
                type="textarea"
                placeholder="请输入评测描述"
                v-model="infoForm.description"
                maxlength="200"
                show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {assessCateAdd, assessCateDetail, assessCateEdit, assessAdd, assessDetail, assessEdit} from "@/api/admin/evaluating"

export default {
  props: ['visible', 'detailType', 'detailId', 'cateId'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      dialogVisible: false,
      infoForm: {
        name: '',
        type: 1,
        description: ''
      },
      section_ids: [],
      tableList: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.getDetailFn()
        this.InitFn()
      } else {
        this.infoForm = {
          name: '',
          type: 1,
          description: ''
        }
        this.section_ids = []
      }
    }
  },
  created() {

  },
  methods: {
    InitFn() {
      this.$post(this.api.admin_section_index, {
        p: 1
      }).then((response) => {
        if (response.code === 0) {
          this.tableList = response.list.data
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getDetailFn() {
      if (Number(this.detailType) === 1 && this.detailId) {
        assessCateDetail({id: this.detailId}).then(res => {
          if (!res.code) {
            this.infoForm.name = res.vo.name
            for(let item of  res.vo.section_ids){
              this.section_ids.push(item.id)
            }
          }
        })
      }
      if (Number(this.detailType) === 2 && this.detailId) {
        assessDetail({id: this.detailId}).then(res => {
          if (!res.code) {
            this.infoForm.name = res.vo.name
            this.infoForm.type = res.vo.type
            this.infoForm.description = res.vo.description
          }
        })
      }
    },
    checkEditFn() {
      console.log('section_ids:', this.section_ids)
      if (!this.infoForm.name) {
        this.$message.error('请填写名称');
        return
      }
      console.log('this.detailType:', this.detailType)
      // 评测类
      let dataOption = {name: this.infoForm.name}
      if (Number(this.detailType) === 1) {
        if (this.detailId) {
          dataOption.id = this.detailId
          dataOption.section_ids = this.section_ids.join(',')
          assessCateEdit(dataOption).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.$emit('InitFn')
              this.closePOpup()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          dataOption.section_ids = this.section_ids.join(',')
          assessCateAdd(dataOption).then(res => {
            if (!res.code) {
              this.$message.success(res.msg);
              this.$emit('InitFn')
              this.closePOpup()
            }
          })
        }
      }
      // 评测细项
      if (Number(this.detailType) === 2) {
        dataOption.description = this.infoForm.description
        dataOption.type = this.infoForm.type
        dataOption.cate_id = this.cateId
        // class_name 	整型 	必填 	1 	- 	名称
        // cate_id 	整型 	必填 	1 	- 	分类id
        // type 	整型 	必填 	1 	- 	类型
        // description 	整型 	必填 	1 	- 	简介
        if (this.detailId) {
          dataOption.id = this.detailId
          assessEdit(dataOption).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.$emit('InitFn')
              this.closePOpup()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          assessAdd(dataOption).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.$emit('InitFn')
              this.closePOpup()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 500px;
  height: 400px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


